<?php

namespace app\admin\controller\selution;

use app\admin\model\Category;
use app\admin\model\Rule;
use app\admin\model\Supplier;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="图片自检")
 */
class Selution extends AdminController
{

    use \app\admin\traits\Curd;

    /**
     * 所有供应商 处理后的数组
     * @var array
     */
    protected $suppliers = [];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Product();
        /*
         * 获取所有供应商 处理后的数组
         */
        $this->suppliers = get_supplier_arr();

    }


    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
//            dd($where);
            $count = $this->model
                ->withJoin(['category', 'supplier', 'rule'], 'LEFT')
//                ->with('suppliers')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin(['category', 'supplier', 'rule'], 'LEFT')
                ->with(['suppliers'])
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            $supplier_name_arr = $this->suppliers;

            $sign_arr = [
                'AI图'=>'AIImg',
                'color'=>'CustomColor ',
                '刻字大图'=>'TextImg',
                '刻字文字'=>'CustomText',
                '生产预览图'=>'ProductPreviewImg',
                '工厂生产图'=>'ProductImg',
                '效果图'=>'EffectImg',
                '原图'=>'SourceImg',
                '字母图'=>'LetterImg',
                '字母文字'=>'LetterAppend',
                '备注'=>'Memo',
                '刻字效果图'=>'TextEffectImg',
            ];

            $list = collection($list->toArray())->map(function ($item, $key) use ($supplier_name_arr,$sign_arr) {

                if (isset($item['suppliers'])) {
                    foreach ($item['suppliers'] as $key => $v) {
                        $item['suppliers'][$key] = $supplier_name_arr[$v['supplier_id']];
                    }
                }
                //获取ai图相关定制字段 处理json 添加到列表中
                $ai_custom = json_decode($item['ai_custom'],true);

                $item['Memo'] = $ai_custom['memo']??'';
                $item['LetterAppend'] = $ai_custom['letter_append']??'';
                $item['CustomText'] = $ai_custom['custom_text']??'';
                $item['CustomColor'] = $ai_custom['custom_color']??'';

                collection($ai_custom)->pluck('url','label')->filter()->map(function ($v,$k)use(&$item,$sign_arr){
                    $item[$sign_arr[$k]] = $v;
                });
                //添加ai图相关定制字段 结束

                return $item;

            })->toArray();


            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }

        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="导出")
     */
    public function export()
    {
        list($page, $limit, $where) = $this->buildTableParames();

        !empty($where) ? ($where[0][0] == 'id' ? $where[0][0] = $this->model->getName() . '.id' : '') : [];

        $list = $this->model
            ->withJoin(['category', 'supplier', 'rule','adminuser'], 'LEFT')
            ->with(['suppliers'])
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        $supplier_name_arr = $this->suppliers;

        $sign_arr = [
            'AI图'=>'AIImg',
            'color'=>'CustomColor ',
            '刻字大图'=>'TextImg',
            '刻字文字'=>'CustomText',
            '生产预览图'=>'ProductPreviewImg',
            '工厂生产图'=>'ProductImg',
            '效果图'=>'EffectImg',
            '原图'=>'SourceImg',
            '字母图'=>'LetterImg',
            '字母文字'=>'LetterAppend',
            '备注'=>'Memo',
            '刻字效果图'=>'TextEffectImg',
        ];

        $list = collection($list->toArray())->map(function ($item, $key) use ($supplier_name_arr,$sign_arr) {

            if (isset($item['suppliers'])) {
                foreach ($item['suppliers'] as $key => $v) {
                    $item['suppliers'][$key] = $supplier_name_arr[$v['supplier_id']];
                }
            }
            //获取ai图相关定制字段 处理json 添加到列表中
            $ai_custom = json_decode($item['ai_custom'],true);

            $item['Memo'] = $ai_custom['memo']??'';
            $item['LetterAppend'] = $ai_custom['letter_append']??'';
            $item['CustomText'] = $ai_custom['custom_text']??'';
            $item['CustomColor'] = $ai_custom['custom_color']??'';

            collection($ai_custom)->pluck('url','label')->filter()->map(function ($v,$k)use(&$item,$sign_arr){
                $item[$sign_arr[$k]] = $v;
            });
            //添加ai图相关定制字段 结束

            return $item;

        })->toArray();

        $data = [];

//        dd($list);
        $header = [
            ['唯一码', 'unique_code'],
            ['货号', 'sku'],
            ['工厂', 'bn'],
            ['状态', 'status'],
            ['刻字大图', 'TextImg'],
            ['刻字效果图', 'TextEffectImg'],
            ['生产预览图', 'ProductPreviewImg'],
            ['工厂生产图', 'ProductImg'],
            ['效果图', 'EffectImg'],
            ['原图', 'SourceImg'],
            ['字母文字', 'LetterAppend'],
            ['操作人', 'ai_author'],
            ['AI异常原因', 'ai_error'],
            ['工厂异常原因', 'sup_error'],
            ['备注', 'Memo'],
            ['ERP留言', 'erp_msg'],
            ['操作时间', 'ai_time'],
            ['对接时间', 'ai_time'],
            ['ERP分类', 'erp_type'],

        ];
        collection($list)->map(function ($item, $key) use (&$data) {
            $data[$key]['unique_code'] = $item['unique_code'];
            $data[$key]['sku'] = $item['sku'];
            $data[$key]['bn'] = $item['supplier']['bn'];
            $data[$key]['status'] = $item['status'];
            $data[$key]['TextImg'] = $item['TextImg']??'';
            $data[$key]['TextEffectImg'] = $item['TextEffectImg']??'';
            $data[$key]['ProductPreviewImg'] = $item['ProductPreviewImg']??'';
            $data[$key]['ProductImg'] = $item['ProductImg']??'';
            $data[$key]['EffectImg'] = $item['EffectImg']??'';
            $data[$key]['SourceImg'] = $item['SourceImg']??'';
            $data[$key]['LetterAppend'] = $item['LetterAppend']??'';
            $data[$key]['ai_author'] = $item['adminuser']['username']??'';
            $data[$key]['ai_error'] = '';
            $data[$key]['sup_error'] = '';
            $data[$key]['Memo'] = $item['Memo'];
            $data[$key]['erp_msg'] = '';
            $data[$key]['ai_time'] = $item['ai_time'];
            $data[$key]['erp_type'] = '';

        });

        $title = '图片自检数据';
        $fileName = $title . '-' . date('Ymd', time());

        return Excel::exportData($data, $header, $fileName, 'xlsx');
    }

    /**
     * @NodeAnotation(title="统计导出")
     */
    public function count_url()
    {
        list($page, $limit, $where) = $this->buildTableParames();

    }

    /**
     * @NodeAnotation(title="条件导出")
     */
    public function where_url()
    {
        list($page, $limit, $where) = $this->buildTableParames();

    }
    /**
     * @NodeAnotation(title="批量导出")
     */
    public function batch_url()
    {
        list($page, $limit, $where) = $this->buildTableParames();

    }

    /**
     * @NodeAnotation(title="AI时间导出")
     */
    public function AI_url()
    {
        list($page, $limit, $where) = $this->buildTableParames();

    }
    /**
     * @NodeAnotation(title="批量修改状态")
     */
    public function batch_change_url()
    {
        list($page, $limit, $where) = $this->buildTableParames();

    }



}